package com.etl.dataflow.scheduler.admin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.etl.dataflow.common.response.R;
import com.etl.dataflow.scheduler.admin.entity.DataflowTaskGroupInfo;
import com.etl.dataflow.scheduler.admin.entity.request.PageRequest;
import com.etl.dataflow.scheduler.admin.service.DataflowTaskGroupInfoService;
import com.etl.dataflow.scheduler.admin.validation.Add;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

/**
 * <p>
 * 任务组信息 前端控制器
 * </p>
 *
 * @author dx
 * @since 2022/6/20
 */
@RestController
@RequestMapping("/admin/task-group-info")
public class DataflowTaskGroupInfoController implements DefaultController {
    private final DataflowTaskGroupInfoService taskGroupInfoService;

    public DataflowTaskGroupInfoController(DataflowTaskGroupInfoService taskGroupInfoService) {
        this.taskGroupInfoService = taskGroupInfoService;
    }

    @PostMapping("/addOrUpdate")
    public R add(@RequestBody @Validated(Add.class) DataflowTaskGroupInfo groupInfo) {
        return successOrError(taskGroupInfoService.saveOrUpdate(groupInfo));
    }

    @GetMapping("/remove")
    public R remove(@RequestParam @NotNull Long id) {
        return successOrError(taskGroupInfoService.removeById(id));
    }

    @GetMapping("/find")
    public R find(@RequestParam @NotEmpty String id) {
        DataflowTaskGroupInfo taskInfo = taskGroupInfoService.getById(id);
        return R.success().put("data", taskInfo);
    }

    @PostMapping("/paging")
    public R paging(@RequestBody @Validated PageRequest pageRequest) {
        Page<? extends DataflowTaskGroupInfo> page = taskGroupInfoService.page(pageRequest);
        return R.success().put("data", page);
    }

    @GetMapping("/search")
    public R search(@RequestParam String keyword) {
        return taskGroupInfoService.search(keyword);
    }

    @GetMapping("/runGroup")
    public R runGroup(@RequestParam @NotNull Long groupId) {
        return taskGroupInfoService.runGroup(groupId);
    }

    @GetMapping("/enableGroupTimer")
    public R enableTimer(@RequestParam @NotNull Long groupId) {
        return taskGroupInfoService.enableGroupTimer(groupId);
    }

    @GetMapping("/disableGroupTimer")
    public R disableTimer(@RequestParam @NotNull Long groupId) {
        return taskGroupInfoService.disableGroupTimer(groupId);
    }
}
